<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎登录图书商城</view>
			<u-input class="u-border-bottom" type="text" v-model="email" placeholder="请输入邮箱" height="80" />
			<u-input class="u-border-bottom" type="password" v-model="password" placeholder="请输入密码" height="80" />
			<!-- <button @tap="throttleLogin()" :style="[inputStyle]" class="getCaptcha">登录</button> -->
			<button @tap="$u.utils.throttle(login,1000)()" :style="[inputStyle]" class="getCaptcha">登录</button>
			<view class="alternative">
				<view class="password">找回密码</view>
				<view class="issue" @tap="goRegister">注册</view>
			</view>
		</view>
		
	</view>
</template>

<script>
// import {throttle} from "@/common/utils.js"
export default {
	data() {
		return {
			email: 'test@a.com',
			password:'123123'
		}
	},
	computed: {
		inputStyle() {
			let style = {};
			if(this.$u.test.email(this.email) && this.password) {
				style.color = "#fff";
				style.backgroundColor = this.$u.color['warning'];
			}
			return style;
		},
		// throttleLogin(){
		// 	return this.$u.utils.throttle(this.login,1000)
		// }
	},
	methods: {
		// throttleLogin:throttle(function(){
		// 	this.login()
		// },1000),
		goRegister(){
			this.$u.route({
				type:'navigateTo',
				url:'pages/auth/register'
			})
		},
		async login() {
			
			if(!this.email) return this.$u.toast("邮箱不能为空")
			if(!this.$u.test.email(this.email)) return this.$u.toast("邮箱输入不正确")
			if(!this.password) return this.$u.toast("密码不能为空")
			if(this.password.length<6) return this.$u.toast("密码至少是6个字符")
			const params={
				email:this.email,
				password:this.password
			}
			const loginRes = await this.$u.api.authLogin(params)
			
			
			this.$u.vuex('vuex_token',loginRes.access_token)
			this.$u.toast("登录成功")
			
			const userInfo = await this.$u.api.getUserInfo()
			this.$u.vuex('vuex_user',userInfo)
			console.log(userInfo)	
			//登录之后，跳转回来源页
			const backUrl=uni.getStorageSync('back_url') || 'pages/index/index'
			setTimeout(()=>{
				this.$u.route({
					type:'reLaunch',
					url:backUrl
				})
			},1500)
		}
	}
};
</script>

<style lang="scss" scoped>
.wrap {
	font-size: 28rpx;
	.content {
		width: 600rpx;
		margin: 80rpx auto 0;

		.title {
			text-align: left;
			font-size: 60rpx;
			font-weight: 500;
			margin-bottom: 100rpx;
		}
		.u-border-bottom {
			
			padding:0 25rpx !important;
			margin-bottom: 50rpx;
			
			background-color: #eee;
			
			border-radius: 15rpx;
		}
		.tips {
			color: $u-type-info;
			margin-bottom: 60rpx;
			margin-top: 8rpx;
		}
		.getCaptcha {
			background-color: rgb(253, 243, 208);
			color: $u-tips-color;
			border: none;
			font-size: 30rpx;
			padding: 12rpx 0;
			
			&::after {
				border: none;
			}
		}
		.alternative {
			color: $u-tips-color;
			display: flex;
			justify-content: space-between;
			margin-top: 30rpx;
		}
	}
	
}
</style>


